// pages/user/order/record/record.js
import {
  getInvoiceList
} from '../../../../api/order.js'
import { removeBadge } from '../../../../api/other.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoiceBadge: [],
    page: 1,
    list: [],
    rowCount: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.invoiceBadge = getApp().globalData.unRead.invoice;
    this.getData();
  },

  getData() {
    getInvoiceList(this.data.page).then(res => {
      // 判断是否显示红点
      res.list = res.list.map(item => {
        if (this.data.invoiceBadge.includes(item.id)) {
          item.badge = true;
        } else {
          item.badge = false;
        }
        return item;
      })
      this.setData({
        list: this.data.list.concat(res.list),
        rowCount: res.rowCount
      })
    })
  },

  jumpToDetail(e) {
    const {
      id
    } = e.currentTarget.dataset;
    this.clearBadge(id);
    wx.navigateTo({
      url: '/pages/user/order/record/details?id=' + id,
    })
  },

  /**
   * 消除红点
   * @param {number} id 发票申请编号
   */
  clearBadge(id) {
    getApp().globalData.unRead.invoice = this.data.invoiceBadge.filter(item => item !== id);
    let list = this.data.list.map(item => {
      if (item.id === id) {
        item.badge = false;
      }
      return item;
    })
    removeBadge(7, id);
    this.setData({
      list
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.rowCount > this.data.list.length) {
      this.data.page += 1;
      this.getData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})